package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.SalesOrderAudit;
import cn.stylefeng.guns.modular.system.model.params.BothSaleCommitAuditParam;
import cn.stylefeng.guns.modular.system.model.params.SalesOrderAuditParam;
import cn.stylefeng.guns.modular.system.service.SalesOrderAuditService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * 控制器
 *
 * @author 陶启然
 * @Date 2019-07-30 16:01:24
 */
@Controller
@RequestMapping("/salesOrderAudit")
public class SalesOrderAuditController extends BaseController {

    private String PREFIX = "/salsOrderCheck";

    @Autowired
    private SalesOrderAuditService salesOrderAuditService;

    @Resource
    private TaskService taskService;

    /**
     * 跳转到主页面
     *
     * @author 陶启然
     * @Date 2019-07-30
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/salesOrderAudit.html";
    }

    /**
     * 新增页面
     *
     * @author 陶启然
     * @Date 2019-07-30
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/salesOrderAudit_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 陶启然
     * @Date 2019-07-30
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/salesOrderAudit_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 陶启然
     * @Date 2019-07-30
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(SalesOrderAuditParam salesOrderAuditParam) {
        this.salesOrderAuditService.add(salesOrderAuditParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 陶启然
     * @Date 2019-07-30
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(SalesOrderAuditParam salesOrderAuditParam) {
        this.salesOrderAuditService.update(salesOrderAuditParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 陶启然
     * @Date 2019-07-30
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(SalesOrderAuditParam salesOrderAuditParam) {
        this.salesOrderAuditService.delete(salesOrderAuditParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 陶启然
     * @Date 2019-07-30
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(SalesOrderAuditParam salesOrderAuditParam) {
        SalesOrderAudit detail = this.salesOrderAuditService.getById(salesOrderAuditParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 陶启然
     * @Date 2019-07-30
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(SalesOrderAuditParam salesOrderAuditParam) {
        return this.salesOrderAuditService.findPageBySpec(salesOrderAuditParam);
    }

    /**
     * 跳转到审核页面
     * @return
     */
    @RequestMapping("/shenheIndex")
    public String shenheIndex(String taskId,Long indentOrderId){
        super.setAttr("taskId",taskId);
        super.setAttr("salesOrderId",indentOrderId);
        return  PREFIX + "/salsOrderCheck_edit.html";
    }

    /**
     * 跳转到审核页面
     * @return
     */
    @RequestMapping("/checkIndex")
    public String checkIndex(String taskId,Long indentOrderId){
        super.setAttr("taskId",taskId);
        super.setAttr("salesOrderId",indentOrderId);
        return  PREFIX + "/specialCheck.html";
    }

    /**
     * 跳转到发货页面
     * @return
     */
    @RequestMapping("/sendIndex")
    public String sendIndex(String taskId,Long indentOrderId){
        super.setAttr("taskId",taskId);
        super.setAttr("salesOrderId",indentOrderId);
        return  PREFIX + "/salsOrderCheck_edit2.html";
    }

    @RequestMapping("/check")
    @ResponseBody
    public ResponseData check(SalesOrderAuditParam salsOrderCheckParam) {

        SalesOrderAudit salesOrderAudit = new SalesOrderAudit();
        BeanUtils.copyProperties(salsOrderCheckParam, salesOrderAudit);
        salesOrderAudit.setAuditTime(new Date());
        salesOrderAudit.setUserId(ShiroKit.getUserNotNull().getId());
        Task task = taskService.createTaskQuery().taskId(salsOrderCheckParam.getTaskId()).singleResult();
        salesOrderAudit.setProcessInstanceId(task.getProcessInstanceId());
        salesOrderAuditService.save(salesOrderAudit);

        //启动审核流程
        Map<String, Object> map = new HashMap<>();
        map.put("status", salsOrderCheckParam.getAuditStatus());
        taskService.complete(task.getId(), map);

        return ResponseData.success(200, "审核成功", salesOrderAudit);
    }

    @RequestMapping("/back")
    public  String back(){

        return  PREFIX + "/salsOrderCheck.html";
    }

    @RequestMapping("/getSpecialBack")
    public String getSpecialBack() {
        return PREFIX + "/specialSalsOrderCheck.html";
    }
}


